#ifndef BINARYSEARCHTREETEST_H
#define BINARYSEARCHTREETEST_H

#include <QtCore>
#include <stdio.h>
#include <QVector>

#include "binarysearchtree.h"

namespace sk {

template <class T>
void myVisit(Node<T>* const node, int level) {
    printf("%d (%d)", node->value(), level);
}

template <class T>
void printTree(Node<T>* const node, int level) {
    static int iCurrentLevel = -1;

    if (level != iCurrentLevel) {
        if (iCurrentLevel != -1) printf("\n");
        printf("Level %d: ", level);
        iCurrentLevel = level;
    }
    printf("%d ", node->value());
}

class BinarySearchTreeTest : public QObject {
    Q_OBJECT

public:
    BinarySearchTreeTest(QObject *parent = 0) : QObject(parent) {}

public slots:
    void run() {
        printf("BST test started\n");

        printf("Adding tree numbers\n");
        BinarySearchTree<int> bst;
        bst.insert(5); bst.insert(3); bst.insert(7); bst.insert(4); bst.insert(2); bst.insert(1); bst.insert(8); bst.insert(6);

        printf("size: %d\n", bst.size());
        printf("depth: %d\n", bst.depth());

        printf("Breadth-first search: ");
        bst.breadthFirst(myVisit<int>);
        printf("\n");

        printf("Depth-first search, preorder: ");
        bst.depthFirstSearch(myVisit<int>, AbstractSearchTree::DFST_PREORDER);
        printf("\n");

        printf("Depth-first search, inorder: ");
        bst.depthFirstSearch(myVisit<int>, AbstractSearchTree::DFST_INORDER);
        printf("\n");

        printf("Depth-first search, postorder: ");
        bst.depthFirstSearch(myVisit<int>, AbstractSearchTree::DFST_POSTORDER);
        printf("\n");

        printf("Printing breadth-first search: \n");
        bst.breadthFirst(printTree<int>);
        printf("\n");
        emit finished();
    }

signals:
    void finished();
};

}

#endif // BINARYSEARCHTREETEST_H
